import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { GitRevisionPlugin } from 'git-revision-webpack-plugin';
import path from 'path';
import { defineConfig } from 'vite';
import serverConfig from './config/server';
import lessVarsConfig from './config/less-vars';
import resolve from 'rollup-plugin-node-resolve';
const gitRevisionPlugin = new GitRevisionPlugin();

export default defineConfig({
  resolve: {
    alias: [
      { find: '@', replacement: path.resolve(__dirname, './src') },
      {
        find: '@ajax',
        replacement: path.resolve(__dirname, 'src/library/util/api/ajax'),
      },
      { find: /^~/, replacement: '' },
    ],
  },

  css: {
    preprocessorOptions: {
      less: {
        modifyVars: lessVarsConfig,
        javascriptEnabled: true,
      },
    },
  },

  server: serverConfig,
  plugins: [vue(), vueJsx()],
  base: `./`,

  // 引入第三方的配置
  optimizeDeps: {
    include: ['echarts', 'axios', 'dayjs'],
  },
  build: {
    // sourcemap: true,
    brotliSize: false,
    chunkSizeWarningLimit: 1000, //限制打包尺寸
    terserOptions: {
      /**
       * command 用来判断环境
       */
      compress: {
        drop_console: true,
        pure_funcs: ['console.log'], //移除console
      },
    },

    rollupOptions: {
      plugins: [resolve()],
      output: {
        chunkFileNames: 'js/[name].[hash].js',
        entryFileNames: 'js/[name].[hash].js',
        manualChunks(id) {
          // 分包
          if (id.includes('node_modules')) {
            return id
              .toString()
              .split('node_modules/')[1]
              .split('/')[0]
              .toString();
          }
        },
      },
    },
  },
});
